package com.frxuexi.myvideo.view

import android.os.Bundle
import android.view.Gravity
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.PopupWindow
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.PagerSnapHelper
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.lib_base.entity.MyVideoCommentBeanItem
import com.frxuexi.lib_base.mvvm.BaseFragment
import com.frxuexi.myvideo.MyVideoViewModel
import com.frxuexi.myvideo.R
import com.frxuexi.myvideo.adapter.MyVideoAdapter
import com.frxuexi.myvideo.adapter.MyVideoCommentAdapter
import com.frxuexi.myvideo.databinding.FragmentMyVideoBinding
import com.frxuexi.myvideo.databinding.PopMycommentBinding
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class MyVideoFragment : BaseFragment<FragmentMyVideoBinding,MyVideoViewModel>() {
    val myVideoAdapter by lazy { MyVideoAdapter() }
    val myVideoCommentAdapter by lazy { MyVideoCommentAdapter() }
    lateinit var myTwoCommentAdapter:MyVideoCommentAdapter
    val comments= mutableListOf<MyVideoCommentBeanItem>()
    var position=0
    var vid=0
    var isHui=false
    var parentId=-1

    lateinit var popBinding:PopMycommentBinding
    override fun initData() {
        vm.myVideoData.observe(this){
            myVideoAdapter.apply {
                data.clear()
                addData(it)
                notifyDataSetChanged()
            }
            //查询第一个视频的评论
            vid=it.get(0).id
            vm.getMyComments(vid)
        }

        vm.addLoveData.observe(this){
//            val imageView = myVideoAdapter.getViewByPosition(position, R.id.iv_love) as ImageView
//            imageView.setImageResource(com.frxuexi.lib_base.R.drawable.ic_xihuanred)
//            val tv = myVideoAdapter.getViewByPosition(position, R.id.tv_love) as TextView

            myVideoAdapter.data.get(position).dianzan=1
            myVideoAdapter.notifyItemChanged(position)
        }

        vm.cancelLoveData.observe(this){
            myVideoAdapter.data.get(position).dianzan=0
            myVideoAdapter.notifyItemChanged(position)
        }

        vm.myCommentsData.observe(this){
            comments.clear()
            comments.addAll(it.filter { !it.msg.isNullOrEmpty() }.sortedByDescending { it.createtime })
            val tv_comment = myVideoAdapter.getViewByPosition(position, R.id.tv_comment) as TextView
            tv_comment.setText(comments.size.toString())

            myVideoCommentAdapter.apply {
                myVideoCommentAdapter.data.clear()
                data.clear()
                addData(comments)
                notifyDataSetChanged()
            }
        }

        vm.myTwoCommentsData.observe(this){
            myTwoCommentAdapter.apply {
                data.clear()
                val tows = it.filter { !it.msg.isNullOrEmpty() }
                addData(tows)
                notifyDataSetChanged()
            }
        }

        vm.addMyCommentData.observe(this){
            myVideoCommentAdapter.data.add(0,it.get(0))
            myVideoCommentAdapter.notifyItemInserted(0)
        }

        vm.huiCommentData.observe(this){
            ToastUtils.showLong("回复成功")
//            if(isZhan) {
//                myTwoCommentAdapter.addData(0, it.get(0))
//                myTwoCommentAdapter.notifyItemInserted(0)
//            }
        }

        vm.addGuanData.observe(this){
            myVideoAdapter.data.get(position).guanzhu=1
            myVideoAdapter.notifyItemChanged(position)
        }

        vm.cancelGuanData.observe(this){
            myVideoAdapter.data.get(position).guanzhu=0
            myVideoAdapter.notifyItemChanged(position)
        }

    }

    override fun initView() {
        val manager = LinearLayoutManager(requireContext())
        binding.videoRv.apply {
            adapter=myVideoAdapter
            layoutManager=manager
        }
        vm.getMyVideos(3,10)

        //仿抖音翻页
        PagerSnapHelper().attachToRecyclerView(binding.videoRv)

        binding.videoRv.setOnScrollChangeListener { v, scrollX, scrollY, oldScrollX, oldScrollY ->
            position = manager.findFirstVisibleItemPosition()
            val gsy =
                myVideoAdapter.getViewByPosition(position, R.id.gsy) as StandardGSYVideoPlayer
            gsy.startPlayLogic()
            //查询第一个视频的评论
            vid=myVideoAdapter.data.get(position).id
            vm.getMyComments(vid)
        }

        myVideoAdapter.addChildClickViewIds(R.id.iv_love,R.id.iv_comment,R.id.iv_userIcon)
        myVideoAdapter.setOnItemChildClickListener { adapter, view, position ->
            val video = myVideoAdapter.data.get(position)
            when(view.id){
                R.id.iv_love->{
                    if(video.dianzan==0){
                        vm.addLove(video.id)
                    }else{
                        vm.cancelLove(video.id)
                    }
                }
                R.id.iv_comment->{
                    val window = PopupWindow(ViewGroup.LayoutParams.MATCH_PARENT, 1000)
                    popBinding = PopMycommentBinding.inflate(LayoutInflater.from(requireContext()))
                    window.contentView=popBinding.root
                    window.isFocusable=true

                    window.showAtLocation(view,Gravity.BOTTOM,0,0)
                    popBinding.rvComment.apply {
                        this.adapter=myVideoCommentAdapter
                        layoutManager=LinearLayoutManager(requireContext())
                    }
                    popBinding.etcount.setText("评论数量:${comments.size}")
                    popBinding.colose.setOnClickListener {
                        window.dismiss()
                    }

                    popBinding.fasong.setOnClickListener {
                        val con = popBinding.et.text.toString()
                        if(isHui){
                            vm.huiComment(mapOf("videoId" to video.id,"parentId" to parentId,"msg" to con))
                        }else {
                            vm.addMyComment(mapOf("videoId" to video.id, "msg" to con))
                        }
                        popBinding.et.setText("")
                    }
                }
                R.id.iv_userIcon->{//关注、取消关注
                    if(video.guanzhu==0){//关注
                        vm.addGuan(mapOf("authname" to video.authname))
                    }else{
                        vm.cancelGuan(mapOf("authname" to video.authname))
                    }
                }
            }
        }

        myVideoCommentAdapter.addChildClickViewIds(R.id.comm_zhan,R.id.comm_hui)
        myVideoCommentAdapter.setOnItemChildClickListener { adapter, view, position ->
            when(view.id){
                R.id.comm_zhan->{
                    myTwoCommentAdapter= MyVideoCommentAdapter()
                    val comment = myVideoCommentAdapter.data.get(position)
                    vm.getMyTwoComments(1,10,comment.id,vid)
                    val rvTwo =
                        myVideoCommentAdapter.getViewByPosition(position, R.id.comm_rv) as RecyclerView
                    rvTwo.apply {
                        this.adapter=myTwoCommentAdapter
                        layoutManager=LinearLayoutManager(requireContext())
                    }
                }
                R.id.comm_hui->{
                    val comment = myVideoCommentAdapter.data.get(position)
                    popBinding.et.hint="回复${comment.username}:"
                    isHui=true
                    parentId=comment.id
                }
            }

        }


    }

}